package com.yuandian.middle.platform.utils;

import com.yuandian.po.MergeField;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
 * 模块名称：自定义字段计算规则-比特率、字节率、包率 适用
 * 开发人员：tianlr
 * 日        期：2022/02/18
 */

public class CustomCalculateRate implements MergeField.CustomCalculate {
    private String filedName;

    public CustomCalculateRate(String filedName){
        this.filedName = filedName;
    }

    @Override
    public Double calculate(Map<String, List<Double>> map, Integer inx, List<Long> labelTimeUsecList, Integer scale) {
        if (map != null && map.size() > 0) {
            List<Double> list = map.get(this.filedName + "List");
            if (list != null && list.size() > 0){
                Double dle = list.get(inx);
                if ("currentBytesTotalPer".equals(this.filedName) || "day1BytesTotalPer".equals(this.filedName)
                        || "day7BytesTotalPer".equals(this.filedName)) {
                    dle = dle * 8;
                }
                if (dle != 0 && scale != null && scale.intValue() != 0) {
                    return new BigDecimal((double) dle / scale).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                }
                return dle;
            }
        }
        return 0.0;
    }

}
